package com.quanyue.haircut.system.controller;

import com.quanyue.auto.fs.StoreService;
import com.quanyue.auto.util.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(tags = "文件接口")
@RestController
public class FileController {

    @Autowired
    private StoreService storeService;

    @ApiOperation(value = "上传图片", produces = MediaType.MULTIPART_FORM_DATA_VALUE)
    @PostMapping(value = {"/api/image/file/upload", "/api/file/upload"})
    public ServiceResult <String> upload(@RequestParam("file") MultipartFile file) throws IOException, MimeTypeException {
        MimeType type = MimeTypes.getDefaultMimeTypes().forName(file.getContentType());
        return ServiceResult.success(storeService.saveFile(10000, file.getBytes(), type.getExtension()));
    }
}
